package com.jic.purchase.service.impl;

import java.lang.Long;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.ManagerStoreApi;
import com.jic.manager.response.MemStoreResponse;
import com.jic.product.store.MStoreApi;
import com.jic.product.store.request.MStoreShopSyncStockRequest;
import com.jic.purchase.mapper.PurchaseContractMapper;
import com.jic.purchase.mapper.PurchaseOrderMapper;
import com.jic.purchase.mapper.PurchaseOrderProductMapper;
import com.jic.purchase.mapper.PurchaseOrderProductOperationMapper;
import com.jic.purchase.model.entity.PurchaseContract;
import com.jic.purchase.model.entity.PurchaseOrder;
import com.jic.purchase.model.entity.PurchaseOrderProduct;
import com.jic.purchase.model.entity.PurchaseOrderProductOperation;
import com.jic.purchase.model.vo.request.PurchaseOrderProductRequest;
import com.jic.purchase.service.PurchaseOrderProductService;
import com.jic.purchase.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购单和商品的关联表
 * @author :
 * @email :
 * @since : 2020-09-19 05:13:42
 * @version : v1.0.0
 */
@Service
public class PurchaseOrderProductServiceImpl implements PurchaseOrderProductService {

    private PurchaseOrderProductMapper purchaseOrderProductMapper;
    private PurchaseContractMapper purchaseContractMapper;
    private PurchaseOrderProductOperationMapper purchaseOrderProductOperationMapper;
    private PurchaseOrderMapper purchaseOrderMapper;
    private MStoreApi mStoreApi;
    private ManagerStoreApi managerStoreApi;

    @Override
    public long insert(PurchaseOrderProduct purchaseOrderProduct) {
        return purchaseOrderProductMapper.insert(purchaseOrderProduct);
    }

    @Override
    public long delete(Long id) {
        return purchaseOrderProductMapper.deleteByPrimaryKey(id);
    }

    @Override
    public long update(PurchaseOrderProduct purchaseOrderProduct) {
        return purchaseOrderProductMapper.update(purchaseOrderProduct);
    }

    @Override
    public PurchaseOrderProduct queryByPrimaryKey(Long id){
        return purchaseOrderProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PurchaseOrderProduct> query(PurchaseOrderProduct purchaseOrderProduct){
        return purchaseOrderProductMapper.listPageBySelective(purchaseOrderProduct);
    }

    @Override
    public PageResult<PurchaseOrderProduct> queryPage(PurchaseOrderProduct purchaseOrderProduct, Page pageQuery) {
        PageHelper.startPage(pageQuery.getStart(), pageQuery.getLimit());
        PageHelper.orderBy(" create_time desc");
        Long count = purchaseOrderProductMapper.listPageBySelectiveCount(purchaseOrderProduct);
        List<PurchaseOrderProduct> responseList = purchaseOrderProductMapper.listPageBySelective(purchaseOrderProduct);
        PageResult<PurchaseOrderProduct> result = new PageResult<>();
        result.setTotal(count);
        result.setRows(responseList);
        return  result;
    }

    @Override
    public long foreachInsert(PurchaseOrderProductRequest request) {
        PurchaseContract purchaseContract = purchaseContractMapper.getPurchaseContractInfo(new PurchaseContract(){{setCode(request.getContractCode());}});
        List<PurchaseOrderProduct> productList = request.getProductList();
        productList.forEach(x->{
            x.setContractId(purchaseContract.getId());
            x.setPurchaseOrderId(request.getPurchaseOrderId());
            x.setPurchaseOrderCode(request.getPurchaseOrderCode());
        });
        return purchaseOrderProductMapper.foreachInsert(productList);
    }

    /**
     * 采购单商品收发货  如果是收货同步库存
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public long editProductNum(PurchaseOrderProductRequest request) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectByPrimaryKey(request.getPurchaseOrderId());
        PurchaseOrderProduct purchaseOrderProduct = BeanUtil.copy(request, PurchaseOrderProduct.class);
        if( 1 == request.getEditType()){
            purchaseOrderProduct.setRealDeliveryNum(purchaseOrderProduct.getRealDeliveryNum()+request.getEditNum());
            purchaseOrderProductMapper.update(purchaseOrderProduct);

            PurchaseOrderProductOperation purchaseOrderProductOperation = BeanUtil.copy(purchaseOrderProduct, PurchaseOrderProductOperation.class);
            purchaseOrderProductOperationMapper.insert(purchaseOrderProductOperation);
        }
        if(2 == request.getEditType()){
            purchaseOrderProduct.setRealDeliveryNum(purchaseOrderProduct.getRealDeliveryNum()+request.getEditNum());
            purchaseOrderProductMapper.update(purchaseOrderProduct);
            PurchaseOrderProductOperation purchaseOrderProductOperation = BeanUtil.copy(purchaseOrderProduct, PurchaseOrderProductOperation.class);
            purchaseOrderProductOperationMapper.insert(purchaseOrderProductOperation);
            RestResult<MemStoreResponse> result =  managerStoreApi.getStoreInfo(purchaseOrder.getStoreId());
            //更新对应库存
            MStoreShopSyncStockRequest syncStockRequest = new MStoreShopSyncStockRequest();
            syncStockRequest.setStoreName(purchaseOrder.getStoreName());
            syncStockRequest.setProductBarNo(request.getProductBarNo());
            syncStockRequest.setProductCode(request.getProductCode());
            syncStockRequest.setProductName(request.getProductName());
            //todo 实际入库数量不是收货数量
            syncStockRequest.setRealRecievedNum(request.getRealDeliveryNum());
            String storeCode = "0000".equals(result.getCode()) && null != result.getData() ? result.getData().getStoreCode() : "";
            syncStockRequest.setStoreCode(storeCode);
            mStoreApi.syncStock(syncStockRequest);
        }
        return 0;
    }

    @Autowired
    public void setPurchaseOrderProductMapper(PurchaseOrderProductMapper purchaseOrderProductMapper) {
        this.purchaseOrderProductMapper = purchaseOrderProductMapper;
    }
    @Autowired
    public void setPurchaseContractMapper(PurchaseContractMapper purchaseContractMapper) {
        this.purchaseContractMapper = purchaseContractMapper;
    }
    @Autowired
    public void setPurchaseOrderProductOperationMapper(PurchaseOrderProductOperationMapper purchaseOrderProductOperationMapper) {
        this.purchaseOrderProductOperationMapper = purchaseOrderProductOperationMapper;
    }
    @Autowired
    public void setPurchaseOrderMapper(PurchaseOrderMapper purchaseOrderMapper) {
        this.purchaseOrderMapper = purchaseOrderMapper;
    }
    @Autowired
    public void setmStoreApi(MStoreApi mStoreApi) {
        this.mStoreApi = mStoreApi;
    }
    @Autowired
    public void setManagerStoreApi(ManagerStoreApi managerStoreApi) {
        this.managerStoreApi = managerStoreApi;
    }
}